package com.example.demo.juc;

import sun.dc.pr.PRError;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo {

    private  static  final  int CORE_POO_SIZE = 5;
    private  static  final  int  MAX_POOL_SIZE = 10;
    private  static  final  int QUEUE_CAPACITY = 100;
    private  static  final  Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) {

        /**
         * 创建线程池
         */
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POO_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i <10 ; i++) {
            //创建WorkThread对象(WorkThread类实现了Runnable接口)
            Runnable worker = new MyRunnable(""+i);
            //执行Runnable
            executor.execute(worker);
        }

        //终止线程池
        executor.shutdown();
        while (!executor.isTerminated()){
        }
        System.out.println("Finished all threads");

    }
}
